<style>
.divmask {
    pointer-events: none;
}
</style>

<template>
    <div class="auto">
      <a href="#" class="btn btn-primary" @click="this.$back('items')">&lt;&lt;</a>
      <a href="#" v-show="actionEnabled" class="btn btn-success" @click="savePaper(true)">保存</a>
      <a href="#" v-show="actionEnabled" class="btn btn-primary" @click="uploadPaper">上传</a>
      <div>
        <radio-group :value.sync="selector" type="info" buttons="false">
          <radio value="paper-state-noncivil">安检概况</radio>
          <radio value="paper-user-noncivil">用户信息</radio>
          <radio value="paper-device-noncivil">设备信息</radio>
          <radio value="device-defect">隐患汇总</radio>
          <radio value="paper-feedback">用户反馈</radio>
        </radio-group>
      </div>
      <route :comp="{name: 'void-page'}" name="paper-router"></route>
      <modal :show.sync="showModal">
        <div slot="modal-header" class="modal-header">
          <h4 class="modal-title">
            照片浏览
          </h4>
        </div>
        <div slot="modal-body" class="modal-body">
          <img :src="pic_to_be_viewed" width="420" height="560">
        </div>
        <div slot="modal-footer" class="modal-footer">
          <button type="button" class="btn btn-success" @click="showModal = false">确认</button>
        </div>
      <modal>
    </div>
</template>

<script>
import RadioGroup from 'vue-strap/src/radioGroup'
import Radio from 'vue-strap/src/radioBtn'
import Vue from 'vue'
import * as Util from '../Util'

export default {
  title: '工商安检单',
  props: ['f_plan_id', 'item', 'role'],
  data() {
    return {
      data: {
      },
      timer: null,
      showModal: false,
      pic_to_be_viewed: ''
    }
  },
  components: {
    RadioGroup,
    Radio
  },
  computed: {
    selector: {
      get() {
        return 'paper-state'
      },
      set(val) {
        this.$goto(val, {}, 'paper-router')
      }
    },
    actionEnabled: {
      get() {
        return this.role != 'view'
      }
    }
  },
  methods: {
    viewPic(fileName) {
      console.log('view pic: ' + fileName)
      if(this.role == 'view') {
        if(!fileName.endsWith('nopic.png')) {
          this.pic_to_be_viewed = fileName
          this.showModal = true
        }
      } else {
        HostApp.viewPic(fileName)
      }
    },
    tick() {
      this.timer = null
      this.data.f_offsite_time = Util.toStandardTimeString()
      this.timer = window.setTimeout(this.tick, 1000)
    },
    getRepairState() {
      let repaired = true
      this.data.f_defects.forEach((defect) => {
        repaired = repaired & (defect.f_ratification == '已处理')
      })
      return repaired
    },
    verifyRepairDefects() {
      this.data.f_repair_date = Util.toStandardTimeString()
      this.data.f_repaired = this.getRepairState() ? '已修' : '在修'
    },
    savePaper(saveOnly) {
      if(this.role == 'repair') {
        let errorMsg = this.verifyRepairDefects()
        if(errorMsg) {
          HostApp.alert(errorMsg)
          return false
        }
        this.save(saveOnly)
        return true
      } else {
        //保存时不做校验
        let errString = saveOnly ? false : this.errorVerify()
        if(errString) {
          console.log(errString)
          HostApp.alert(errString)
          return false
        } else {
          errString = this.collectDefects()
          if(!saveOnly && errString == 1) {
            //使用HostApp总报错，改为window
            window.__callback__ = this.genuineSave
            window.__this__ = this
            console.log('回调:' + window.__callback__)
            HostApp.confirm('本次安检存在安全隐患，真的不需要指定维修人吗？', 'javascript:window.__callback__(' +saveOnly + ');')
            return
          }
          errString = saveOnly ? false : errString
          //如果上传要检验
          if(errString) {
            console.log(errString)
            HostApp.alert(errString)
            return false
          }
          this.save(saveOnly)
        }
        this.disableTimer()
        return true
      }
    },
    genuineSave(saveOnly) {
      window.__this__.save(saveOnly)
      if(!saveOnly)
        window.__this__.disableTimer()
      window.__callback__ = null
      window.__this__ = null
    },
    uploadPaper() {
      this.savePaper(false)
    },
    errorVerify() {
      let hasError = this.verifyState()
      if(hasError)
        return hasError
      hasError = this.verifyUser()
      if(hasError)
        return hasError
      hasError = this.verifyDevices()
      if(hasError)
        return hasError
      hasError = this.verifyFeedback()
      if(hasError)
        return hasError
    },
    verifyState() {
      if(this.data.f_stop_service && Util.required(this.data.f_stop_devices) && Util.required(this.data.f_close_valve) &&
        Util.required(this.data.f_stop_devices) && Util.required(this.data.f_close_valve) &&
        Util.required(this.data.f_unload_battery) && Util.required(this.data.f_zero_meter) &&
        Util.required(this.data.f_cutoff_gas) && Util.required(this.data.f_empty_pipe) &&
        Util.required(this.data.f_close_adjuster))
        return '停气必须选择停气措施'
      if(Vue.android && this.data.f_close_valve && this.data.f_close_valve_path == Vue.nopic)
        return '停气关闭表前阀必须拍照'
      if(Vue.android && this.data.f_cutoff_gas && this.data.f_cutoff_gas_path == Vue.nopic)
        return '停气切断气源必须拍照'
      if(Vue.android && this.data.f_close_adjuster && this.data.f_close_adjuster_path == Vue.nopic)
        return '停气关闭表前阀必须拍照'
    },
    verifyUser() {
      let prefix = '用户基本信息：'
      if(Util.required(this.data.f_user_name))
        return prefix + '客户名称必须输入'
      if(Util.required(this.data.f_user_phone))
        return prefix + '联系电话必须输入'
      if(Util.required(this.data.f_address))
        return prefix + '地址必须输入'
    },
    verifyMeters() {
      let usage = 0
      //abrupt exit
      for(var i=0; i < this.data.f_meters.length; i++) {
        let item = this.data.f_meters[i]
        let prefix = '燃气表' + (i + 1) + '：'
        if(Util.required(item.f_meter_type))
          return prefix + '缺少燃气表类型'
        if(Util.required(item.f_maker))
          return prefix + '缺少生产厂家'
        if(Util.required(item.f_making_date))
          return prefix + '缺少生产年份'
        if(!Util.isInt(item.f_making_date))
          return prefix + '生产年份不是整数'
        if(Vue.android && item.f_working_state && item.f_working_state_path == Vue.nopic)
          return prefix + '缺少工作状态异常照片'
        if(Util.required(item.f_meter_erosion))
          return prefix + '缺少腐蚀选项'
        if(Vue.android && item.f_meter_erosion == '严重' && item.f_erosion_path == Vue.nopic)
          return prefix + '缺少严重腐蚀照片'
        if(Vue.android && item.f_invalid && item.f_invalid_path == Vue.nopic)
          return prefix + '缺少报废照片'
        if(Util.required(item.f_diaphgram_size))
          return prefix + '缺少表型'
        if(Util.required(item.f_flow_direction))
          return prefix + '缺少进气方向'
        if(item.f_meter_type == '流量计' && Util.required(item.f_traffic_model))
          return prefix + '缺少流量计型号'
        if(Util.required(item.f_calibrate_date))
          return prefix + '缺少检验日期'
        if(Util.required(item.f_calibrate_period))
          return prefix + '缺少检验周期'
        if(!Util.isInt(item.f_calibrate_period))
          return prefix + '检验周期不是整数'
        if(Util.required(item.f_card_id))
          return prefix + '缺少卡号'
        //  if(item.f_meter_type != '流量计' && Util.required(item.f_meter_no))
        if(Util.required(item.f_meter_no))
          return prefix + '缺少基表号'
        if(Util.required(item.f_prior_reading))
          return prefix + '缺少上期基表读数'
        if(Util.required(item.f_usage))
          return prefix + '缺少本期用气量'
        if(item.f_usage / 1 < 0)
            return prefix + '本期用气量不能小于0'
        if(Util.required(item.f_reading))
          return prefix + '缺少基表数'
        if(!Util.isInt(item.f_reading))
          return prefix + '机表数不是整数'
        if(Util.required(item.f_remaining_gas))
          return prefix + '缺少剩余气量'
        if(!Util.isInt(item.f_remaining_gas))
          return prefix + '剩余气量不是整数'
        if(Vue.android && item.f_remaining_gas_path == Vue.nopic)
          return prefix + '缺少剩余气量照片'
        if(Util.required(item.f_defect_no) && Util.required(item.f_defect_magnetic)
          && Util.required(item.f_defect_wrapped) && Util.required(item.f_defect_covered)
          && Util.required(item.f_defect_oil) && Util.required(item.f_defect_unfixed))
          return prefix + '缺少运行环境选项'
        if(Vue.android && item.f_defect_magnetic && item.f_magnetic_path == Vue.nopic)
          return prefix + '缺少强磁照片'
        if(Vue.android && item.f_defect_wrapped && item.f_wrapped_path == Vue.nopic)
          return prefix + '缺少包裹照片'
        if(Vue.android && item.f_defect_covered && item.f_covered_path == Vue.nopic)
          return prefix + '缺少暗闭照片'
        if(Vue.android && item.f_defect_oil && item.f_oil_path == Vue.nopic)
          return prefix + '缺少油污照片'
        if(Vue.android && item.f_defect_unfixed && item.f_unfixed_path == Vue.nopic)
          return prefix + '缺少固定不牢靠照片'
        if(Util.required(item.f_meter_defect))
          return prefix + '缺少表故障选项'
        if(Vue.android && item.f_meter_defect == '其他' && item.f_meter_defect_path == Vue.nopic)
          return prefix + '缺少表故障照片'
        usage += item.f_usage / 1
      }
      //本期总用气量
      this.data.f_usage = usage
    },
    verifyPlumbs() {
      //abrupt exit
      for(var i=0; i < this.data.f_plumbs.length; i++) {
        let item = this.data.f_plumbs[i]
        let prefix = '立管' + (i + 1) + '：'
        if(Util.required(item.f_plumb_normal) && Util.required(item.f_plumb_trapped)
           && Util.required(item.f_plumb_hung) && Util.required(item.f_plumb_modified)
           && Util.required(item.f_plumb_nearpower) && Util.required(item.f_plumb_loose)
           && Util.required(item.f_plumb_oil) && Util.required(item.f_plumb_tramp)
           && Util.required(item.f_plumb_viberate))
          return prefix + '缺少安检项'
        if(Vue.android && item.f_plumb_trapped && item.f_trapped_path == Vue.nopic)
          return prefix + '缺少暗封照片'
        if(Vue.android && item.f_plumb_hung && item.f_hung_path == Vue.nopic)
          return prefix + '缺少异物悬挂、缠绕照片'
        if(Vue.android && item.f_plumb_modified && item.f_modified_path == Vue.nopic)
          return prefix + '缺少私改照片'
        if(Vue.android && item.f_plumb_nearpower && item.f_nearpower_path == Vue.nopic)
          return prefix + '缺少与电源过近照片'
        if(Vue.android && item.f_plumb_loose && item.f_loose_path == Vue.nopic)
          return prefix + '缺少固定不牢靠照片'
        if(Vue.android && item.f_plumb_oil && item.f_oil_path == Vue.nopic)
          return prefix + '缺少油污照片'
        if(Vue.android && item.f_plumb_tramp && item.f_tramp_path == Vue.nopic)
          return prefix + '缺少踩踏照片'
        if(Vue.android && item.f_plumb_viberate && item.f_viberate_path == Vue.nopic)
          return prefix + '缺少震动照片'
        if(Util.required(item.f_plumb_erosion))
          return prefix + '缺少腐蚀选项'
        if(Vue.android && item.f_plumb_erosion == '严重' && item.f_erosion_path == Vue.nopic)
          return prefix + '缺少严重腐蚀照片'
        if(Util.required(item.f_plumb_outer_leakage))
          return prefix + '缺少外漏选项'
        if(Util.required(item.f_plumb_inner_leakage))
          return prefix + '缺少内漏选项'
        if(Util.required(item.f_plumb_pressure))
          return prefix + '缺少测试压力'
        if(!Util.isInt(item.f_plumb_pressure))
          return prefix + '测试压力不是整数'
      }
    },
    verifyValves() {
      //abrupt exit
      for(var i=0; i < this.data.f_valves.length; i++) {
        let item = this.data.f_valves[i]
        let prefix = '阀门' + (i + 1) + '：'
        if(Util.required(item.f_valve_meter_normal) && Util.required(item.f_valve_meter_leakage) && Util.required(item.f_valve_meter_wrapped))
          return prefix + '缺少表前阀安检项'
        if(item.f_valve_meter_wrapped && item.f_meter_wrapped_path == Vue.nopic)
          return prefix + '缺少表前阀包裹照片'
        if(Util.required(item.f_valve_meter_on))
          return prefix + '缺少表前阀开关状态'
        if(Util.required(item.f_valve_cooker_normal) && Util.required(item.f_valve_cooker_leakage) && Util.required(item.f_valve_cooker_wrapped))
          return prefix + '缺少灶前阀安检项'
        if(item.f_valve_cooker_wrapped && item.f_cooker_wrapped_path == Vue.nopic)
          return prefix + '缺少灶前阀包裹照片'
        if(Util.required(item.f_valve_cooker_on))
          return prefix + '缺少灶前阀开关状态'
        if(Util.required(item.f_valve_terminal_normal) && Util.required(item.f_valve_terminal_leakage) && Util.required(item.f_valve_terminal_wrapped))
          return prefix + '缺少截止阀安检项'
        if(item.f_valve_terminal_wrapped && item.f_terminal_wrapped_path == Vue.nopic)
          return prefix + '缺少截止阀包裹照片'
        if(Util.required(item.f_valve_magnetic_normal) && Util.required(item.f_valve_magnetic_leakage) && Util.required(item.f_valve_magnetic_wrapped))
          return prefix + '缺少电磁阀安检项'
        if(item.f_valve_magnetic_wrapped && item.f_magnetic_wrapped_path == Vue.nopic)
          return prefix + '缺少电磁阀包裹照片'
      }
    },
    verifyAlarms() {
      //abrupt exit
      for(var i=0; i < this.data.f_alarms.length; i++) {
        let item = this.data.f_alarms[i]
        let prefix = '报警器' + (i + 1) + '：'
        if(Util.required(item.f_alarm_inuse))
          return prefix + '缺少安装选项'
        if(item.f_alarm_inuse == '未安装' && item.f_inuse_path == Vue.nopic)
          return prefix + '缺少未安装照片'
        if(item.f_alarm_inuse == '未安装')
          return
        if(Util.required(item.f_brand))
          return prefix + '缺少品牌选项'
        if(Util.required(item.f_make_date))
          return prefix + '缺少生产日期'
        if(Util.required(item.f_calibrate_date))
          return prefix + '缺少检验日期'
        if(item.f_uncalibrated && item.f_uncalibrated_path == Vue.nopic)
          return prefix + '缺少未校验照片'
        if(Util.required(item.f_working_state))
          return prefix + '缺少工作状态'
        if(item.f_working_state == '异常' && item.f_working_state_path == Vue.nopic)
          return prefix + '缺少工作状态异常照片'
        if(Util.required(item.f_sensor_state))
          return prefix + '缺少探头工作状态'
        if(item.f_sensor_state == '异常' && item.f_sensor_state_path == Vue.nopic)
          return prefix + '缺少探头工作状态异常照片'
        if(Util.required(item.f_sensor_setup))
          return prefix + '缺少探头安装状态'
        if(item.f_sensor_setup == '不合格' && item.f_sensor_setup_path == Vue.nopic)
          return prefix + '缺少探头安装异常照片'
        if(Util.required(item.f_explosive_proof))
          return prefix + '缺少防爆设施选项'
        if(item.f_explosive_proof == '无' && item.f_proof_path == Vue.nopic)
          return prefix + '缺少无防爆设施照片'
      }
    },
    verifyCookers() {
      //abrupt exit
      for(var i=0; i < this.data.f_cookers.length; i++) {
        let item = this.data.f_cookers[i]
        let prefix = '燃气具' + (i + 1) + '：'
        if(Util.required(item.f_approval))
          return prefix + '缺少合格证选项'
        if(Util.required(item.f_making_date))
          return prefix + '缺少投产日期'
        if(Util.required(item.f_pipe_material))
          return prefix + '缺少连接方式'
        if(item.f_pipe_material == '防爆挠性管' && item.f_material_path == Vue.nopic)
          return prefix + '缺少防爆挠性管照片'
        if(item.f_pipe_material == '橡胶软管' && item.f_material_path == Vue.nopic)
          return prefix + '橡胶软管'
      }
    },
    verifyFurnaces() {
      //abrupt exit
      for(var i=0; i < this.data.f_furnaces.length; i++) {
        let item = this.data.f_furnaces[i]
        let prefix = '锅炉' + (i + 1) + '：'
        if(Util.required(item.f_brand))
          return prefix + '缺少品牌'
        if(Util.required(item.f_making_date))
          return prefix + '缺少生产日期'
        if(Util.required(item.f_check_date))
          return prefix + '缺少年检日期'
        if(Util.required(item.f_check_period))
          return prefix + '缺少检验周期'
        if(!Util.isInt(item.f_check_period))
          return prefix + '检验周期不是整数'
        if(Util.required(item.f_pressure_label))
          return prefix + '缺少工作压力'
        if(!Util.isFloat(item.f_pressure_label))
          return prefix + '工作压力不是数字'
        if(Util.required(item.f_pressure_operation))
          return prefix + '缺少运行压力'
        if(!Util.isFloat(item.f_pressure_operation))
          return prefix + '运行压力不是数字'
        if(Util.required(item.f_thermal_power))
          return prefix + '缺少热功率'
        if(!Util.isFloat(item.f_thermal_power))
          return prefix + '热功率不是数字'
        if(Util.required(item.f_ventilation_state))
          return prefix + '缺少自然通风选项'
        if(item.f_ventilation_state == '异常' && item.f_ventilation_state_path == Vue.nopic)
          return prefix + '缺少自然通风异常照片'
        if(Util.required(item.f_resonance))
          return prefix + '缺少设备共振选项'
        if(Util.required(item.f_connectivity))
          return prefix + '缺少设备连接选项'
      }
    },
    verifyDevices() {
      if(!this.data.f_meters || this.data.f_meters.length == 0)
        return '请检查燃气表'
      let hasError = null
      if(this.data.f_meters && this.data.f_meters.length > 0)
        hasError = hasError || this.verifyMeters()
      if(hasError)
        return hasError
      if(!this.data.f_plumbs || this.data.f_plumbs.length == 0)
        return '请检查燃气管道'
      if(this.data.f_plumbs && this.data.f_plumbs.length > 0)
        hasError = hasError || this.verifyPlumbs()
      if(hasError)
        return hasError
      if(!this.data.f_valves || this.data.f_valves.length == 0)
        return '请检查阀门'
      if(this.data.f_valves && this.data.f_valves.length > 0)
        hasError = hasError || this.verifyValves()
      if(hasError)
        return hasError
      // if(!this.data.f_alarms || this.data.f_alarms.length == 0)
      //   return '请检查报警器'
      if(this.data.f_alarms && this.data.f_alarms.length > 0)
        hasError = hasError || this.verifyAlarms()
      if(hasError)
        return hasError
      if(this.data.f_cookers && this.data.f_cookers.length > 0)
        hasError = hasError || this.verifyCookers()
      if(hasError)
        return hasError
      if(this.data.f_furnaces && this.data.f_furnaces.length > 0)
        hasError = hasError || this.verifyFurnaces()
      if(hasError)
        return hasError
    },
    verifyFeedback() {
      let prefix = '用户反馈：'
      if(Vue.android && (!this.data.f_overall_path || this.data.f_overall_path == Vue.nopic) && this.data.f_entry_status == '入户')
          return prefix + '必须有安检总体照片'
      if(Vue.android && this.data.f_defect_notified && this.data.f_notified_path == Vue.nopic)
        return prefix + '下发安检通知书则必须给通知书拍照'
      if(!this.data.f_client_evaluation && this.data.f_entry_status == '入户')
          return prefix + '请选择服务评价'
      if(Vue.android && !this.data.f_recording1_path && this.data.f_entry_status == '入户')
          return prefix + '安检必须录音'
    },
    collectDefects() {
      // 维修时，不再提取数据了
      if(this.role != 'inspect')
        return
      this.$set('data.f_defects', [])
      let defects = this.data.f_defects
      // 燃气表隐患
      if(this.data.f_meters) {
        this.data.f_meters.forEach(function(item, index) {
          let pre = '燃气表' + (index+1)
          if(item.f_meter_erosion && item.f_meter_erosion == '严重')
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '严重腐蚀', f_defect_level: 'C2', f_defect_path: item.f_erosion_path})
          if(item.f_invalid)
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '报废', f_defect_level: 'C18', f_defect_path: item.f_invalid_path})
          if(item.f_defect_magnetic)
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '强磁', f_defect_level: 'C30', f_defect_path: item.f_magnetic_path})
          if(item.f_defect_wrapped)
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '包裹', f_defect_level: 'C32', f_defect_path: item.f_wrapped_path})
          if(item.f_defect_covered)
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '暗闭', f_defect_level: 'C33', f_defect_path: item.f_covered_path})
          if(item.f_defect_oil)
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '油污', f_defect_level: 'C31', f_defect_path: item.f_oil_path})
          if(item.f_defect_unfixed)
            defects.push({f_device_type: '燃气表', f_device_id: pre, f_defect: pre + '固定不牢靠', f_defect_level: 'C11', f_defect_path: item.f_unfixed_path})
        })
      }
      // 立管
      if(this.data.f_plumbs) {
        this.data.f_plumbs.forEach(function(item, index) {
          let pre = '立管' + (index+1)
          if(item.f_plumb_trapped)
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '暗封', f_defect_level: 'C12', f_defect_path: item.f_trapped_path})
          if(item.f_plumb_hung)
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '异物悬挂、缠绕', f_defect_level: 'C15', f_defect_path: item.f_hung_path})
          if(item.f_plumb_modified)
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '私改', f_defect_level: 'B7', f_defect_path: item.f_modified_path})
          if(item.f_plumb_nearpower)
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '与电源过近', f_defect_level: 'C4', f_defect_path: item.f_nearpower_path})
          if(item.f_defect_unfixed)
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '固定不牢靠', f_defect_level: 'C11', f_defect_path: item.f_loose_path})
          if(item.f_plumb_oil)
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '油污', f_defect_level: 'C32', f_defect_path: item.f_oil_path})
          if(item.f_plumb_tramp)
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '踩踏', f_defect_level: 'C34', f_defect_path: item.f_tramp_path})
          if(item.f_plumb_viberate)
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '震动', f_defect_level: 'C35', f_defect_path: item.f_viberate_path})
          if(item.f_plumb_erosion && item.f_plumb_erosion == '严重')
            defects.push({f_device_type: '立管', f_device_id: pre, f_defect: pre + '严重腐蚀', f_defect_level: 'C1', f_defect_path: item.f_erosion_path})
        })
      }
      // 阀门
      if(this.data.f_valves) {
        this.data.f_valves.forEach(function(item, index) {
          let pre = '阀门' + (index+1)
          if(item.f_valve_meter_leakage)
            defects.push({f_device_type: '阀门', f_device_id: '表前阀', f_defect: pre + '漏气', f_defect_level: 'A1'})
          if(item.f_valve_meter_wrapped)
            defects.push({f_device_type: '阀门', f_device_id: '表前阀', f_defect: pre + '包裹', f_defect_level: 'B17', f_defect_path: item.f_meter_wrapped_path})
          if(item.f_valve_cooker_leakage)
            defects.push({f_device_type: '阀门', f_device_id: '灶前阀', f_defect: pre + '漏气', f_defect_level: 'A1'})
          if(item.f_valve_cooker_wrapped)
            defects.push({f_device_type: '阀门', f_device_id: '灶前阀', f_defect: pre + '包裹', f_defect_level: 'B17', f_defect_path: item.f_cooker_wrapped_path})
          if(item.f_valve_terminal_leakage)
            defects.push({f_device_type: '阀门', f_device_id: '截至阀', f_defect: pre + '漏气', f_defect_level: 'A1'})
          if(item.f_valve_terminal_wrapped)
            defects.push({f_device_type: '阀门', f_device_id: '截至阀', f_defect: pre + '包裹', f_defect_level: 'B17', f_defect_path: item.f_terminal_wrapped_path})
          if(item.f_valve_magnetic_leakage)
            defects.push({f_device_type: '阀门', f_device_id: '电磁阀', f_defect: pre + '漏气', f_defect_level: 'A1'})
          if(item.f_valve_magnetic_wrapped)
            defects.push({f_device_type: '阀门', f_device_id: '电磁阀', f_defect: pre + '包裹', f_defect_level: 'B17', f_defect_path: item.f_magnetic_wrapped_path})
        })
      }
      // 报警器
      if(this.data.f_alarms) {
        this.data.f_alarms.forEach(function(item, index) {
          let pre = '报警器' + (index+1)
          if(item.f_alarm_inuse && item.f_alarm_inuse == '未安装')
            defects.push({f_device_type: '报警器', f_device_id: pre, f_defect: pre + '报警器未安装', f_defect_level: 'A8', f_defect_path: item.f_inuse_path})
          else {
            if(item.f_uncalibrated)
              defects.push({f_device_type: '报警器', f_device_id: pre, f_defect: pre + '未校验', f_defect_level: 'B12', f_defect_path: item.f_uncalibrated_path})
            if(item.f_working_state && item.f_working_state == '异常')
              defects.push({f_device_type: '报警器', f_device_id: pre, f_defect: pre + '工作状态异常', f_defect_level: 'A9', f_defect_path: item.f_working_state_path})
            if(item.f_explosive_proof && item.f_explosive_proof == '无')
              defects.push({f_device_type: '报警器', f_device_id: pre, f_defect: pre + '无防爆设施', f_defect_level: 'C36', f_defect_path: item.f_proof_path})
          }
        })
      }
      // 灶具
      if(this.data.f_cookers) {
        this.data.f_cookers.forEach(function(item, index) {
          let pre = '燃气具' + (index+1)
          if(item.f_pipe_material && item.f_pipe_material == '防爆挠性管')
            defects.push({f_device_type: '燃气具', f_device_id: pre, f_defect: pre + '连接为防爆挠性管', f_defect_level: 'C57', f_defect_path: item.f_material_path})
          if(item.f_pipe_material && item.f_pipe_material == '橡胶软管')
            defects.push({f_device_type: '燃气具', f_device_id: pre, f_defect: pre + '连接为橡胶软管', f_defect_level: 'C6', f_defect_path: item.f_material_path})
        })
      }
      // 锅炉
      if(this.data.f_furnaces) {
        this.data.f_furnaces.forEach(function(item, index) {
          let pre = '锅炉' + (index+1)
          if(item.f_ventilation_state && item.f_ventilation_state == '异常')
            defects.push({f_device_type: '锅炉', f_device_id: pre, f_defect: pre + '自然通风异常', f_defect_level: 'C3', f_defect_path: item.f_ventilation_state_path})
        })
      }
      this.setUrgency()
      if(defects.length >0) {
        if(!this.data.f_repairman) {
          return 1
        }
      } else {
        if(this.data.f_repairman) {
          return "不存在安全隐患，不需要指定维修人"
        }
      }
    },
    setUrgency() {
      this.data.f_defect_urgency = 'Z'
      let that = this
      this.data.f_defects.forEach(function(defect) {
        if(that.data.f_defect_urgency > defect.f_defect_level.charAt(0))
          that.data.f_defect_urgency = defect.f_defect_level.charAt(0)
      })
      if(this.data.f_defect_urgency == 'Z')
        this.data.f_defect_urgency = null
    },
    save(saveOnly) {
      if(Vue.android) {
        //let data = {"f_check_item_id":"15","f_check_plan_id":"4028811d569c3f7201569c3ffd86000a","f_checker_id":"5858637","f_checker_name":"安检员1","f_check_type":"民用安检","f_onsite_time":"2016-09-17 10:13:56","f_offsite_time":"2016-09-17 10:15:12","f_entry_status":"入户","f_user_name":"q","f_user_phone":"q","f_user_id":"1","f_address":"街道1小区11号楼1单元层11号","f_no_gas_device":false,"f_close_valve":false,"f_unload_battery":false,"f_room_style":"多层","f_heating_style":"热力公司集中供暖","f_nongasuser_path":"file:\/\/\/android_asset\/nopic.png","f_residential_area":"q","f_meters":[{"f_meter_path":"file:\/\/\/storage\/emulated\/0\/safecheck\/undefined.jpg?0.6854880177415907","f_erosion_path":"file:\/\/\/android_asset\/nopic.png","f_wrapped_path":"file:\/\/\/android_asset\/nopic.png","f_card_id":"qq","f_maker":"成都秦川","f_diaphgram_size":"G1.6","f_diaphgram_size_":"","f_reading":1,"f_remaining_gas":1,"f_meter_no":"1","f_making_date":1111,"f_flow_direction":"左表","f_defect_no":true,"f_meter_defect":"正常","f_meter_erosion":"正常"}],"f_plumbs":[{"f_erosion_path":"file:\/\/\/android_asset\/nopic.png","f_trapped_path":"file:\/\/\/android_asset\/nopic.png","f_plumb_normal":true,"f_plumb_erosion":"正常","f_plumb_tightness":"正常","f_plumb_pressuretest":"不合格","f_plumb_pressure":111}],"f_valves":[{"f_valve_meter_normal":true,"f_valve_meter_on":"开","f_valve_auto_normal":true,"f_valve_cooker_normal":true,"f_valve_cooker_on":"开"}],"f_inner_plumbs":[{"f_plumb_normal":true}],"f_alarms":[{"f_alarm_inuse":"未使用","f_mechanic":"电磁","f_brand":"qq","f_model":"qq"}],"f_cookers":[],"f_furnaces":[],"f_heaters":[],"f_notified_path":"file:\/\/\/android_asset\/nopic.png","f_signature_path":"file:\/\/\/android_asset\/nopic.png","f_client_evaluation":"满意","f_recording1_path":"file:\/\/\/storage\/emulated\/0\/safecheck\/15.amr","f_defects":[]}
        //HostApp.simpleTask({type:'logic', hint: '正在保存安检单，请稍等...', data: {alias:'保存安检单', data: {f_check_item_id: this.data.f_check_item_id, f_checker_id: this.data.f_checker_id, row: data}}})
        if(saveOnly)
          HostApp.savePaper({hint: ['正在保存工单，请稍等...', '保存成功！', '保存失败！'], data: {f_check_item_id: this.data.f_check_item_id, f_checker_id: this.data.f_checker_id, row: this.data}})
        else {
          if(this.role == 'repair') {
            HostApp.uploadRepair({f_check_item_id: this.data.f_check_item_id, f_checker_id: this.data.f_checker_id, data: this.data})
          } else {
            HostApp.uploadPaper({f_check_item_id: this.data.f_check_item_id, f_checker_id: this.data.f_checker_id, data: this.data})
          }
        }
      }
    },
    disableTimer() {
      if(this.timer) {
        window.clearTimeout(this.timer)
        this.timer = null
      }
    },
    loaded(paper) {
      if(!paper.hasOwnProperty('id')) {
        HostApp.alert('该安检单在手机上已经不存在，可能是卸载过程序。可以进入重新安检，或返回安检计划。')
        this.init()
      } else {
        Util.booleanFilter(paper)
        HostApp.__this__.$set('data', paper)
        HostApp.__this__.$goto('paper-state-noncivil', {}, 'paper-router')
        // 如果打回重检则重新计时
        if(HostApp.__this__.role == 'inspect' && HostApp.__this__.f_approved && HostApp.__this__.f_approved == '打回') {
          HostApp.__this__.data.f_onsite_time = Util.toStandardTimeString()
          HostApp.__this__.tick()
        }
      }
      HostApp.__callback__ = null
      HostApp.__this__ = null
    },
    init () {
      this.data.f_check_item_id = this.item.id
      this.data.f_check_plan_id = this.f_plan_id
      this.data.f_checker_id = Vue.user.id
      this.data.f_checker_name = Vue.user.name
      this.data.f_check_type = this.item.f_user_type
      this.data.f_residential_area = this.item.f_residential_area
      this.data.f_onsite_time = Util.toStandardTimeString()
      this.data.f_subcompany = this.item.f_subcompany
      this.data.f_approved = this.item.f_approved
      this.data.f_repair_approved = this.item.f_repair_approved
      this.$set('data.f_offsite_time', Util.toStandardTimeString())

      // 安检状态
      this.$set('data.f_entry_status', '入户')

      // 用户信息
      this.$set('data.f_user_name', this.item.f_user_name)
      this.$set('data.f_user_phone', this.item.f_user_phone)
      this.$set('data.f_user_id', this.item.f_user_id)
      this.$set('data.f_address', this.item.f_address)
      this.$set('data.f_card_id', this.item.f_card_id)

      //房屋结构
      this.$set('data.f_room_style', '地上')
      //供暖方式
      this.$set('data.f_noncivil_style', '餐饮用户')

      this.tick()
      this.$goto('paper-state-noncivil', {}, 'paper-router')
    }
  },
  ready() {
    //如果已检，则提取安检单
    if(this.item.f_state != '未检') {
      if(this.role != 'view') {
        HostApp.__callback__ = this.loaded
        HostApp.__this__ = this
      }
      //维修单
      if(this.role == 'repair')
        HostApp.fetchPaper({hint: ['正在提取工单，请稍等...', null, '提取工单失败，该工单可能已经处理并上传！'], callback:'javascript:HostApp.__callback__(%s)', data: {f_check_item_id:this.item.f_check_item_id, f_checker_id: this.item.f_checker_id}})
      else if(this.role == 'inspect')
        HostApp.fetchPaper({hint: ['正在提取工单，请稍等...', null, '提取工单失败，该工单可能已经处理并上传！'], callback:'javascript:HostApp.__callback__(%s)', data: {f_check_item_id:this.item.id, f_checker_id: Vue.user.id}})
      else if(this.role == 'view') {
        let that = this
        this.$post('rs/logic/提取安检单', {data:{id: this.item.id}}, {resolveMsg: null, rejectMsg: null}).then(
          (response) => {
            that.$set('data', response.data)
            Util.booleanFilter(that.data)
            Util.ratifyPics(that.data)
            that.$goto('paper-state', {}, 'paper-router')
          }
        ).catch(
          (response) => {
            window.alert('查找安检单失败')
          }
        )
      }
    }
    else {
      this.init()
    }
  },
  destroyed() {
    this.disableTimer()
  }
}
</script>
